package com.sright.bcard.content.biz.manager.impl;


import com.sright.bcard.common.biz.manager.ContentCheckManager;
import com.sright.bcard.common.service.share.dto.request.TextCheckRequest;
import com.sright.bcard.content.biz.manager.TextCheckManager;
import com.sright.bcard.content.dal.dao.UserDAO;
import com.sright.bcard.content.dal.dao.UserInfoCheckResultDAO;
import com.sright.bcard.content.dal.entity.UserDO;
import com.sright.bcard.content.dal.entity.UserInfoCheckResultDO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

@Component
@Slf4j
public class TextCheckManagerImpl implements TextCheckManager {

    @Autowired
    ContentCheckManager contentCheckService;

    @Autowired
    UserInfoCheckResultDAO userInfoCheckResultDAO;

    @Autowired
    UserDAO userDAO;

    //组合检测敏感词的list
    @Override
    public String textCheck(TextCheckRequest request,Long userId,Integer sourceId) {
        Field[] requestField = request.getClass().getDeclaredFields();

        List<String> nameList = new LinkedList<>();
        List<String> contentList = new LinkedList<>();
        UserInfoCheckResultDO userInfoCheckResultDO = new UserInfoCheckResultDO();
        for (Field field : requestField) {
            String oldFieldName = field.getName();                //源对象属性名
            if (oldFieldName.equals("userId")) continue;
            String fieldName = oldFieldName.substring(0, 1).toUpperCase() + oldFieldName.substring(1);    //属性名第一个字符大写
            Method method = null;           //得到get方法
            try {
                method = request.getClass().getMethod("get" + fieldName);
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }
            Object value = null;                                      //调用getter方法获取属性值
            try {
                assert method != null;
                value = method.invoke(request);
                if (StringUtils.isNotEmpty((CharSequence) value)) {
                    nameList.add(oldFieldName);
                    contentList.add((String) value);
                }
            } catch (IllegalAccessException | InvocationTargetException e) {
                e.printStackTrace();
            }
        }
        if (contentList.size() > 0) {
            //提交文字审核
            List<String> suggestionList = contentCheckService.textCheck1(contentList,userId,sourceId);

            Field[] resultFields = userInfoCheckResultDO.getClass().getDeclaredFields();
            for (Field resultField : resultFields) {
                resultField.setAccessible(true);
                try {
                    if (resultField.getName().equals("userId")) {
                        resultField.set(userInfoCheckResultDO,request.getUserId());
                    } else if (resultField.getName().equals("id") || resultField.getName().equals("createdAt") || resultField.getName().equals("updatedAt")) {
                        resultField.set(userInfoCheckResultDO,null);
                    } else {
                        resultField.set(userInfoCheckResultDO,0);
                        for (int i = 0; i < suggestionList.size(); i++) {

                            String field = nameList.get(i);
                            if (field.equals(resultField.getName())) {
                                if (suggestionList.get(i).equals("review")) {
                                    try {
                                        resultField.set(userInfoCheckResultDO,1);
                                    } catch (IllegalAccessException e) {
                                        e.printStackTrace();
                                    }
                                }
                            }
                        }
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }

            //更新检查结果
            updateCheckTextRecord(userInfoCheckResultDO);
        }

        return "pass";

    }

    public void updateCheckTextRecord(UserInfoCheckResultDO newDO) {
        if (newDO.getUserId() != null) {
            UserInfoCheckResultDO oldDO = userInfoCheckResultDAO.selectByUserId(newDO.getUserId());
            if (oldDO == null) {
                userInfoCheckResultDAO.insertSelective(newDO);
            } else {
                newDO.setId(oldDO.getId());
                userInfoCheckResultDAO.updateSelective(newDO);
            }

            //更新用户状态
            UserInfoCheckResultDO newCheckDO = userInfoCheckResultDAO.selectByUserId(newDO.getUserId());
            updateUserCheckStatus(newCheckDO);
        }
    }

    public void updateUserCheckStatus(UserInfoCheckResultDO checkResultDO) {
        List<String> stringList = Arrays.asList("id","userId","createdAt","updatedAt","isDelete");
        Field[] resultFields = checkResultDO.getClass().getDeclaredFields();
        boolean flag = true;
        for (Field field : resultFields) {
            String oldFieldName = field.getName();                //源对象属性名
            if (stringList.contains(oldFieldName)) continue;
            String fieldName = oldFieldName.substring(0, 1).toUpperCase() + oldFieldName.substring(1);    //属性名第一个字符大写
            Method method = null;           //得到get方法
            try {
                method = checkResultDO.getClass().getMethod("get" + fieldName);
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }
            Integer value = null;                                      //调用getter方法获取属性值
            try {
                assert method != null;
                value = (Integer) method.invoke(checkResultDO);
                if (value == 1) {
                    flag = false;
                }
            } catch (IllegalAccessException | InvocationTargetException e) {
                e.printStackTrace();
            }
        }

        if (!flag) {
            UserDO userDO = userDAO.selectById(checkResultDO.getUserId());
            if (userDO != null) {
                userDO.setCheckStatus(1);
                userDAO.updateSelective(userDO);
            }
        }
    }
}
